package fhp.hlhj.giantfold.activity.user

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.interfaces.ICreateCash
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.BaseBean
import fhp.hlhj.giantfold.javaBean.UserInfoBean
import fhp.hlhj.giantfold.presenter.CreateCashPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.activity_get_cash_aty.*

class GetCashAty : BaseActivity(), ICreateCash, IUser {

    override fun getUser(userInfoBean: UserInfoBean) {
        loadingDialog.dismiss()
        if (userInfoBean.code==200){
            availableMoney.text="可用余额"+MyUtils.formatDouble(userInfoBean.data.balance?.toDouble()!!,3)
        }
    }

    private lateinit var createCashPresenter:CreateCashPresenter
    private lateinit var userPresenter:UserPresenter
    override fun getContentId(): Int = R.layout.activity_get_cash_aty
    override fun initView() {
        userPresenter= UserPresenter(this)
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        userPresenter.getUser(params)
        createCashPresenter= CreateCashPresenter(this)
        availableMoney.text="可用余额"+MyUtils.formatDouble(Contents.userInfo?.balance?.toDouble()!!,3)
    }
    override fun initListener() {
        btCommit.setOnClickListener {
            if (etAliUserName.text.isNotEmpty()){
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("alipay_account",Contents.aliAccount)
                params.put("alipay_user_name",Contents.aliUserName)
                params.put("money",etAliUserName.text.toString().toFloat())
                createCashPresenter.commitCreateCash(params)
            }
        }
        btExit.setOnClickListener {
            finish()
        }
    }
    override fun showLoading() {
        loadingDialog.show()
    }

    override fun createCash(baseBean: BaseBean) {
        loadingDialog.dismiss()
        if (baseBean.code==200){
            MyUtils.toast("申请成功")
            finish()
        }else{
            MyUtils.toast(baseBean.msg)
        }
    }
}
